package vn.bus.bk.adapter;

import java.util.ArrayList;

import vn.bus.bk.R;
import vn.bus.bk.object.BusTwoRouteInfo;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ListTwoPlaceAdapter extends BaseAdapter {
    // private Activity mContext;
    private ArrayList<BusTwoRouteInfo> listTwoBus;
    private LayoutInflater inflater;

    public ListTwoPlaceAdapter(Activity activity,
	    ArrayList<BusTwoRouteInfo> array) {
	// this.mContext = activity;
	this.listTwoBus = array;
	inflater = (LayoutInflater) activity
		.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public int getCount() {
	// TODO Auto-generated method stub
	return listTwoBus.size();
    }

    public Object getItem(int arg0) {
	// TODO Auto-generated method stub
	return arg0;
    }

    public long getItemId(int arg0) {
	// TODO Auto-generated method stub
	return arg0;
    }

    public View getView(int position, View convertView, ViewGroup arg2) {
	// TODO Auto-generated method stub
	BusTwoRouteHolder busHolder = new BusTwoRouteHolder();
	if (convertView == null) {
	    convertView = inflater.inflate(R.layout.item_row_two_bus, null);
	    busHolder.name = (TextView) convertView
		    .findViewById(R.id.tv_name_two_bus);
	    busHolder.first = (TextView) convertView
		    .findViewById(R.id.tv_first_bus);
	    busHolder.second = (TextView) convertView
		    .findViewById(R.id.tv_second_bus);
	    busHolder.pointReplace = (TextView) convertView
		    .findViewById(R.id.tv_point_replaces);
	    convertView.setTag(busHolder);
	}

	final BusTwoRouteInfo b = listTwoBus.get(position);
	if (b != null) {
	    final BusTwoRouteHolder newBusHolder = (BusTwoRouteHolder) convertView
		    .getTag();
	    newBusHolder.name.setText("Liên tuyến "
		    + b.getBusFrom().getNumber() + " - "
		    + b.getBusTo().getNumber());
	    final ArrayList<String> listPlace = b.getPlacesChange();
	    newBusHolder.first.setText(String.valueOf(b.getBusFrom()
		    .getNumber()) + " : " + b.getBusFrom().getName());
	    newBusHolder.second.setText(String
		    .valueOf(b.getBusTo().getNumber())
		    + " : "
		    + b.getBusTo().getName());

	    String places = "Điểm chuyển : ";

	    for (int i = 0; i < listPlace.size() - 1; i++) {
		places = places + listPlace.get(i) + ", ";
	    }
	    places = places + listPlace.get(listPlace.size() - 1);
	    newBusHolder.pointReplace.setText(places);

	}
	return convertView;
    }

}

class BusTwoRouteHolder {
    TextView name, first, second, pointReplace;
}
